package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import lombok.Getter;

import java.io.IOException;
import java.io.Serializable;
import java.util.*;

/**
 * Created by 毛文杰 on 2017/3/27.
 */
@Data
public class DiagnosisReport implements Serializable {
    private static final ObjectMapper objectMapper= JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = -1272141028617826263L;
    /**
     *
     */
    private Long id;
    /**
     *
     */
    private Integer status;
    /**
     *单据编号
     */
    private String number;
    /**
     *养殖场id
     */
    private Long breedFactoryId;
    /**
     *养殖场名称
     */
    private String breedFactoryName;
    /**
     *诊疗人员
     */
    private Long userId;
    /**
     *诊疗人员
     */
    private String userName;
    /**
     *诊疗日期
     */
    private Date date;
    /**
     *养殖场区域
     */
    private Integer area;

    /**
     * 栏舍号id
     */
    private Long stableId;

    /**
     *栏舍号
     */
    private String stableNumber;
    /**
     *圈舍号
     */
    private String barnNumber;
    /**
     *动物种类
     */
    private Integer productType;
    /**
     *病因
     */
    private String illnessReason;
    /**
     *单位
     */
    private Integer unit;
    /**
     *发病数
     */
    private Integer cases;
    /**
     *用药方法
     */
    private Integer drugUsedMethod;
    /**
     *治疗效果：1、康复（头数） 2、死亡（头数）
     */
    @JsonIgnore
    private String result;
    /**
     * 治疗效果：1、康复（头数） 2、死亡（头数）
     */
    private Map<String , Object> resultMap;
    /**
     *禽兽耳标号
     */
    private String earTagNumber;

    /**
     * 耳标号list
     */
    private List<String> earTagNumbers;

    /**
     *用药名称
     */
    private String drugName;
    /**
     *兽药照片
     */
    @JsonIgnore
    private String drugPhoto;
    /**
     * 兽药照片-----不存入数据库
     */
    @Getter
    private Map<String,Map<String,String>> drug;
    /**
     * 休药期
     */
    private String withdrawalPeriod;
    /**
     * 停药日期
     */
    private Date withdrawalDate;

    /**
     *备注
     */
    private String remark;
    /**
     *'1：正常，-1：删除'
     */
    private Integer deleteFlag;
    /**
     *
     */
    private Date createdAt;
    /**
     *
     */
    private Date updatedAt;



    public void setDrugPhoto(String drugPhoto) throws Exception{
        this.drugPhoto = drugPhoto;
        if(Strings.isNullOrEmpty(drugPhoto)){
            this.drug=  Maps.newHashMap();
        } else{
            this.drug = objectMapper.readValue(drugPhoto, new TypeReference<Map<String,Map<String,String>>>(){});
        }
    }
    public void setDrug(Map<String,Map<String,String>> drug) {
        this.drug = drug;
        if(drug ==null ||drug.isEmpty()){
            this.drugPhoto = null;
        }else{
            try {
                this.drugPhoto = objectMapper.writeValueAsString(drug);
            } catch (Exception e) {
            }
        }
    }



    public void setEarTagNumber(String earTagNumber){
        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = new ArrayList<>();
        }else {
            try {
                this.earTagNumbers =
                        objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarTagNumbers(List<String> earTagNumbers){
        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()){
            this.earTagNumbers = null;
        }else {
            try {
                this.earTagNumber =
                        objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }


    public void setResult(String result){
        this.result = result;

        if(Strings.isNullOrEmpty(result)){
            this.resultMap = null;
        }else {
            try {
                this.resultMap = objectMapper.readValue(result, new TypeReference<Map<String , Object>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setResultMap(Map<String , Object> resultMap){
        this.resultMap = resultMap;

        if(resultMap == null || resultMap.isEmpty()){
            this.result = null;
        }else {
            try {
                this.result = objectMapper.writeValueAsString(resultMap);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

}
